﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NHibernate.Linq;

namespace Perpus
{
    public partial class admin_EditKategori : Form
    {
        public admin_EditKategori()
        {
            InitializeComponent();
        }

        NhibernateHelper help = new NhibernateHelper();


        private void button4_Click(object sender, EventArgs e)
        {
            TampilanBuku booklist = new TampilanBuku();
            booklist.Show();
            this.Hide();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var factory = help.CreateSessionFactory();

            using (var session = factory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var category = new Kategori
                    {
                        // kode_buku = int.Parse(textBox1.Text),
                        nama_kategori = namaKategori_textbox.Text
                        //Name = txtCategoryName.Text,
                        //Description = txtCategoryDescription.Text
                    };

                    session.Save(category);
                    transaction.Commit();

                    MessageBox.Show("Berhasil!");
                    dataGridView1.DataSource = dotnetDataSet.Kategori;
                    //this.kategoriTableAdapter.Fill(this.cobaDataSet.Kategori);
                }
            }   
        }

        private void admin_EditKategori_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dotnetDataSet.Kategori' table. You can move, or remove it, as needed.
            this.kategoriTableAdapter.Fill(this.dotnetDataSet.Kategori);
            // TODO: This line of code loads data into the 'cobaDataSet.Kategori' table. You can move, or remove it, as needed.
            

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.RowIndex;

            idKategori_textBox.Text = dataGridView1[0, i].Value.ToString();
            namaKategori_textbox.Text = dataGridView1[1, i].Value.ToString();
            button1.Enabled = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var factory = help.CreateSessionFactory();

            int id = Convert.ToInt32(idKategori_textBox.Text);

            using (var session = factory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var idKategori = session.Get<Kategori>(id);
                    idKategori.nama_kategori = namaKategori_textbox.Text;
                    transaction.Commit();

                }
                MessageBox.Show("Berhasil!");
            }   
        }

        private void clearBtn_Click(object sender, EventArgs e)
        {
            idKategori_textBox.Text = "";
            namaKategori_textbox.Text = "";

            button1.Enabled = true;
        }

       
    }
}
